##################################################
#		Variables			 #
##################################################
# Compilators
C = gcc
OCAMLC = ocamlc
OCAMLOPT = ocamlopt

# Directories
SRC = src/
BIN = bin/

# VPATH Definition
VPATH = $(SRC)

# Flags
#CFLAGS = -O3 -pedantic -W -Wall -Wextra -std=c99

CFLAGS = -Wall -W -std=c99 -pedantic -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Wunreachable-code -D_GNU_SOURCE -DDEBUG #--enable-ext-colors

CXXFLAGS =

# libraries
VIDDEP = libavformat libswscale libavcodec libavutil libpulse-simple
ILDEP = IL

# Depends
CLIDEPS = $(VIDDEP) $(ILDEP)


# ------------------------------------------------ #
#		Includes			   #
# Curses
NCURSESINC = $(shell ncursesw5-config --cflags)
NCURSESLIB = $(shell ncursesw5-config --libs)

# FFmpeg
VIDINC = $(shell pkg-config --cflags $(VIDDEP)) $(NCURSESINC)
VIDLIB = $(shell pkg-config --libs-only-l --libs $(VIDDEP)) $(NCURSESLIB)

# Console Line Interface
CINC = $(shell pkg-config --cflags $(CLIDEPS)) $(NCURSESINC)
CLIB = $(shell pkg-config --libs-only-l --libs $(CLIDEPS)) $(NCURSESLIB)


# ------------------------------------------------ #
#		SRC FILES 			   #

## Ocaml Files
# Sources
MLFILES = color.cmx shellcolor.cmx conv.cmx table.cmx video.cmx
# Headers
OCAMLHDIR = $(shell ocamlopt -where)/caml
MLIFILES = $(patsubst %cmx,%cmi,$(MLFILES))

## C Files
CFILES = thqueue.o libvid.o debug.o display.o interface.o events.o init.o keyboard.o tools.o sound_output.o explorer.o setter.o playlist.o seek.o

# Main File
CLIFILE = aavp.cmx

# Binary Name
CLINAME = aavp

# Names for cleaning
BINNAMES = $(CLINAME)



##################################################
#		RULES				 #
##################################################

# Main
all : cli_nc clean
	@echo -e "\033[31mWarning X: Krisboul is watching you !\033[0m"

nc: cli_nc

bin:
	@mkdir -v bin

cli: command-line-interface clean
cli-nc: cli_nc
cli_nc: command-line-interface

command-line-interface: caml_m $(MLIFILES) $(CFILES) $(MLFILES) $(CLIFILE) bin
	$(OCAMLOPT) -thread unix.cmxa threads.cmxa bigarray.cmxa -cclib -pthread $(addprefix -cclib ,$(CLIB)) $(CFILES) $(MLFILES) $(CLIFILE) -o $(addprefix $(BIN),$(CLINAME))
	@echo -e "\033[35mCommand Line Interface Done\033[0m"

caml:
	@until (ls $(SRC) | grep caml.h > /dev/null); do make caml_m; done

caml_m:
	@echo -e "\033[33mMaking caml.h\033[0m"
	@find $(OCAMLHDIR) -type f 2> /dev/null | \
	sed 's/\(.*\)/#include "\1"/g' > $(SRC)caml.h

#sed i/`find $(OCAMLHDIR) 2> /dev/null`/ | \
        tr -d '\n' | \
        sed 's#.h/#.h\n#g' | \


install: all clean
	cp $(BIN)* /usr/bin
	@echo -e "\033[34mInstallation Done\033[0m"

uninstall: distclean
	rm -f /usr/bin/$(CLINAME)
	@echo -e "\033[34mUninstallation Done\033[0m"

# Suffixes

.SUFFIXES: .ml .mli .cmo .cmx .cmi .o

.ml.cmo:
	$(OCAMLC) -thread -c $<
	@cp  $(patsubst %ml, %cmo,$<) .

.ml.cmx:
	$(OCAMLOPT) -thread -c $<
	@cp  $(patsubst %ml, %cmx,$<) .
	@cp  $(patsubst %ml, %o,$<) .

.mli.cmi:
	$(OCAMLC) -thread -c $<
	@cp  $(patsubst %mli,%cmi,$<) .

.c.o:
	$(C) $(CFLAGS) $(CINC) $(CLIB) -c $<


# Cleaning
.PHONY: clean distclean

clean: FORCE
	@echo -e "\033[33mCleaning\033[0m"
	@rm -f *~
	@rm -f $(SRC)*~
	@rm -f *.cm?
	@rm -f $(SRC)*.cm?
	@rm -f *.o
	@rm -f $(SRC)*.o

distclean: FORCE clean
	@rm -f $(BIN)*
	@rm -rf test

FORCE:

#End of File
